/*
 * 文 件 名:  SpecialAdatper.java
 * 版    权:  Weipu Technologies Co., Ltd. Copyright 2012-2014,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  sjx206
 * 修改时间:  2015-1-6
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.epro.dx.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.URLUtil;
import android.widget.ImageView;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.activity.DetailActivity;
import com.epro.dx.pullview.PullToRefreshGridView;
import com.epro.dx.util.ImageLoaderUtils;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.util.StringUtil;

/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  sjx206
 * @version  [版本号, 2015-1-6]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class SpecialAdapter extends CommonAdapter<ProductModel>
{
    private RotateAnimation rotate = null;
    
    
    private Context context;

	private Tracker tracker;
    
    private int densityDpi;
    
    private DisplayImageOptions options;


	private String currencyCode;


	private String currencyCodeSymbol;
    
    public SpecialAdapter(Context context, List<ProductModel> productList,int densityDpi,Tracker tracker)
    {
        super(productList, context);
        this.context = context;
		this.tracker = tracker;
        rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.setDuration(1080);
        rotate.setStartTime(Animation.START_ON_FIRST_FRAME);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);
        options=ImageLoaderUtils.getImageLoaderOptions(R.drawable.item_default,R.drawable.item_default);
        this.densityDpi=densityDpi;
    }
    
    @Override
    public View getView(final int position, View convertView, final List<ProductModel> list, Context context)
    {
        final ViewHolder viewHolder;
        if (null == convertView)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.special_item, null);
            viewHolder = new ViewHolder();
            viewHolder.itemPic = (ImageView)convertView.findViewById(R.id.item_pic);
            viewHolder.itemPrice = (TextView)convertView.findViewById(R.id.item_price);
            viewHolder.itemTitle = (TextView)convertView.findViewById(R.id.item_title);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        // 获取当前显示列表项商品
        final ProductModel product = (ProductModel)getItem(position);
        // 加载商品图片并给页面图片元素设值
        if (null != product)
        {
            
//            String[] filePath = product.getSmallPictureURL();
        	String[] filePath =getImageUrl(densityDpi, product,177,240);
            if (null != filePath && filePath.length >= 1
                && (URLUtil.isHttpUrl(filePath[0]) || URLUtil.isHttpsUrl(filePath[0])))
            {
            	viewHolder.itemPic.setTag(filePath[0]);
//            	setImageViewByCache(viewHolder.itemPic, filePath[0], imageCache);
                //imageWorker.loadImage(filePath[0], viewHolder.itemPic);
            	ImageLoader.getInstance().displayImage(filePath[0], viewHolder.itemPic, options);
            }
            else
            {
            	
            	viewHolder.itemPic.setImageBitmap(getDefault243(context));
                
            }
            String currency=null;
            if(!TextUtils.isEmpty(currencyCodeSymbol)){
            	currency = currencyCodeSymbol;
            }else{
            	currency = currencyCode;
            }
            viewHolder.itemTitle.setText(product.getShortHeadLine1());
            viewHolder.itemPrice.setText(currency + StringUtil.getPrice(String.valueOf(product.getPrice())));
            
            convertView.setOnClickListener(new OnClickListener()
            {
                
                @Override
                public void onClick(View v)
                {
                    
                    if (list.size() > position)
                    {
                    	tracker.send(new HitBuilders.EventBuilder("Click product","Go to product detail(sku:"+product.getSku()+")").build());
                        detailIntent(list.get(position));
                        
                    }
                }
            });
        }
        return convertView;
    }
    
    static class ViewHolder
    {
        //商品图片
        ImageView itemPic;
        
        //商品价格
        TextView itemPrice;
        
        //商品名称
        TextView itemTitle;
        
    }
    
    /**
     * 跳转详情页面
     * 
     * @param product
     *            产品ID
     */
    protected void detailIntent(ProductModel product)
    {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle data = new Bundle();
        
        data.putInt(ServerConstant.Parameter.PARAMETER_NAME_PRODUCT_ID, Integer.valueOf(product.getSku()));
        data.putString(ServerConstant.Parameter.PARAMETER_NAME_BACK_ACTION, ServerConstant.ActionConstant.ACTION_GADGET);
        data.putString(ServerConstant.Parameter.PARAMETER_NAME_FROM_ACTIVITY,
            ServerConstant.ActionConstant.ACTION_GADGET);
        intent.putExtras(data);
        
        context.startActivity(intent);
    }
    
    public void setCurrency(String currencyCode,String currencyCodeSymbol){
		this.currencyCode = currencyCode;
		this.currencyCodeSymbol = currencyCodeSymbol;
    	
    }
    
	
}
